// OSPSM_ChildProcess.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/offset_ptr.hpp>

using namespace boost::interprocess;

//Shared memory linked list node
struct list_node
{
	offset_ptr<list_node> next;
	int                   value;
};

int main ()
{
	//Remove shared memory on construction and destruction
	struct shm_remove
	{
		shm_remove() { shared_memory_object::remove("MySharedMemory"); }
		~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
	} remover;

	//Create shared memory
	managed_shared_memory segment(create_only, "MySharedMemory", 65536);

	//Create linked list with 10 nodes in shared memory
	offset_ptr<list_node> prev = 0, current, first;

	int i;
	for(i = 0; i < 10; ++i, prev = current)
	{
		current = static_cast<list_node*>(segment.allocate(sizeof(list_node)));
		current->value = i;
		current->next  = 0;

		if(!prev)
			first = current;
		else
			prev->next = current;
	}

	//Communicate list to other processes
	//. . .
	//When done, destroy list
	for(current = first; current; )
	{
		prev = current;
		current = current->next;
		segment.deallocate(prev.get());
	}
	return 0;
}